package cn.santi.controller;

import cn.santi.domain.Staff;
import cn.santi.domain.StaffDate;
import cn.santi.domain.StaffTime;
import cn.santi.domain.StaffUrlpath;
import cn.santi.emuns.R;
import cn.santi.service.StaffDateService;
import cn.santi.service.StaffService;
import cn.santi.service.StaffTimeService;
import cn.santi.service.StaffUrlpathService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/staff")
public class StaffControoler {
    @Autowired
    private StaffService sf;
    @Autowired
    private StaffUrlpathService su;
    @Autowired
    private StaffDateService sd;
    @Autowired
    private StaffTimeService st;

    //获取家政员列表
    @GetMapping("/sellist")
    public R sellist() {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        List<Staff> list = sf.list(wrapper);
        return R.ok(list);
    }

    //根据id查询家政
    @GetMapping("/getbyid")
    public R getbyid(int id) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return R.ok(sf.getOne(wrapper));
    }

    @GetMapping("/geturlpath")
    public R geturlpath(int id) {
        QueryWrapper<StaffUrlpath> wrapper = new QueryWrapper<>();
        wrapper.eq("sid", id);
        List<StaffUrlpath> list = su.list(wrapper);
        return R.ok(list);
    }

    //排序
    @GetMapping("/rank")
    public R rank(int text, String result) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        switch (text) {
            case 1:
                wrapper.orderByDesc("star");
                break;
            case 2:
                wrapper.orderByAsc("star");
                break;
            case 3:
                wrapper.orderByDesc("price");
                break;
            case 4:
                wrapper.orderByAsc("price");
                break;
            case 5:
                wrapper.eq("star", 1);
                break;
            case 6:
                wrapper.eq("star", 2);
                break;
            case 7:
                wrapper.eq("star", 3);
                break;
            case 8:
                wrapper.eq("star", 4);
                break;
            case 9:
                wrapper.eq("star", 5);
                break;
            case 10:
                wrapper.eq("cls", 2);
                break;
            case 11:
                wrapper.eq("cls", 1);
                break;
            case 12:
                wrapper.like("name", result);
                break;
        }
        return R.ok(sf.list(wrapper));
    }

    //根据id获取家政预约时间
    @GetMapping("/getdate")
    public R getdate(int id) {
        QueryWrapper<StaffDate> wrapper = new QueryWrapper<>();
        wrapper.eq("sid", id);
        return R.ok(sd.list(wrapper));
    }

    //查询时间段
    @GetMapping("/gettien")
    public R gettien(int id) {
        QueryWrapper<StaffTime> wrapper = new QueryWrapper<>();
        wrapper.eq("did", id);
        return R.ok(st.list(wrapper));
    }

    //查询时间是否有预约
    @GetMapping("/gettoday")
    public R getToday(String date) {
        QueryWrapper<StaffDate> wrapper = new QueryWrapper<>();
        wrapper.eq("date", date);
        List<StaffDate> staffDateList = sd.list(wrapper);
        List<Integer> staffIds = staffDateList.stream()
                .map(StaffDate::getSid)
                .collect(Collectors.toList());

        if (staffIds.isEmpty()) {
            // 如果staffIds为空，返回空列表或处理这种情况
            return R.ok(Collections.emptyList());
        }

        QueryWrapper<Staff> staffWrapper = new QueryWrapper<>();
        staffWrapper.in("id", staffIds);
        List<Staff> staffList = sf.list(staffWrapper);
        return R.ok(staffList);
    }

    //查询时间段
    @GetMapping("/getperiod")
    public R getperiod(int id) {
        QueryWrapper<StaffTime> wrapper = new QueryWrapper<>();
        wrapper.in("did", id);
        List<StaffTime> staffTimeList = st.list(wrapper);
        return R.ok(staffTimeList);
    }

    @GetMapping("/getDateById")
    public R getDateById(int id){//根据id获取日期
        return R.ok(sd.getById(id));
    }

    @GetMapping("/getTimeById")
    public R getTimeById(int id){//根据id获取时间
        return R.ok(st.getById(id));
    }



    //查询从今天开始到月末有没有可以预约并显示红点
    @GetMapping("/checkDataForDate")
    public R checkDataForDate(String date) {
        QueryWrapper<StaffDate> wrapper = new QueryWrapper<>();
        wrapper.eq("date", date);
        List<StaffDate> staffDateList = sd.list(wrapper);
        if (staffDateList.size() == 0) {
            // 如果staffDateList为空，表示没有数据
            return R.fail("300"); // 返回失败响应，状态码为"300"
        } else {
            // 如果staffDateList不为空，表示有数据
            return R.ok(date);
        }
    }

    //修改密码updatpwd
    @GetMapping("/updatpwd")
    public R updatpwd(int id, String pwd, String setpwd) {
        QueryWrapper<Staff> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id).eq("pwd", pwd);
        Staff one = sf.getOne(wrapper);
        if (one != null) {
            UpdateWrapper<Staff> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", id).set("pwd", setpwd);
            boolean b = sf.update(updateWrapper);
            return R.ok();
        }
        return R.fail(300, "失败");
    }
    //更改图片路径
    @GetMapping("/updataimgpath")
    public R updataimgpath(int id,String imgpath) {
        UpdateWrapper<Staff> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("imgpath",imgpath);
        return R.ok(sf.update(wrapper));
    }
    //删除图片
    @GetMapping("delimgpaht")
    public R delimgpaht(String imgpath,int id) throws IOException {
        Path path = Paths.get("E://hbx/"+imgpath);
        Files.delete(path);
        UpdateWrapper<Staff> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("imgpath","");
        return R.ok(sf.update(wrapper));
    }

}
